package fina2.dcs.fis;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.naming.InitialContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.annotation.ejb.LocalBinding;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;

import fina2.dcs.context.ContextHelper;
import fina2.dcs.i18n.LanguageSession;
import fina2.dcs.system.SysString;
import fina2.dcs.system.SysStringLocal;
import fina2.dcs.system.SysStringPk;

@Stateful
@LocalBinding(jndiBinding = "CriterionSession/local")
@Scope(ScopeType.SESSION)
@SuppressWarnings("unchecked")
@Name("critSession")
@AutoCreate
public class CriterionSession implements CriterionLocal,Serializable {

	@PersistenceContext
	private EntityManager em;

	private List<Criterion> allCriterions = new ArrayList<Criterion>();

	private InitialContext ic = null;

	private SysStringLocal sysStringLocal = null;

	private BankGroupLocal bnkGroupLocal = null;

	private boolean initFirst = false;
	
	@Logger
	private Log log;

	public void init() {
		if (!initFirst) {
			log.info("Initializing Criterions...");
			try {
				ic = ContextHelper.getInitialContext();
				sysStringLocal = ContextHelper.injectSysStringLocal(ic);
				bnkGroupLocal = ContextHelper.injectBankGroupLocal(ic);

				allCriterions = em.createQuery("FROM " + Criterion.class.getName() + " c ORDER BY c.code ASC").getResultList();

				List<BigDecimal> critStrIds = new ArrayList<BigDecimal>();
				for (int i = 0; i < allCriterions.size(); i++) {
					critStrIds.add(allCriterions.get(i).getNameStrId());
				}

				StringBuffer sb = new StringBuffer(critStrIds.toString());
				sb.replace(0, 1, "");
				sb.replace(sb.toString().length() - 1, sb.toString().length(), "");

				List<SysString> descriptions = sysStringLocal.getAllValues(sb.toString(), LanguageSession.langId);

				for (int i = 0; i < allCriterions.size(); i++)
					for (int j = 0; j < descriptions.size(); j++) {
						if (allCriterions.get(i).getNameStrId().toString().trim().equals(descriptions.get(j).getPk().getId().toString().trim())) {
							allCriterions.get(i).setDescription(descriptions.get(j).getValue());
							break;
						}
					}
				for (int i = 0; i < allCriterions.size(); i++) {
					BigDecimal id = allCriterions.get(i).getId();
					allCriterions.get(i).setBankGroups(bnkGroupLocal.getGroupsByCriterionId(id));
				}
				initFirst=true;
			} catch (Exception ex) {
				log.error(ex.getMessage(), ex);
			}
		}

	}

	@Remove
	@Destroy
	public void remove() {

	}

	public List<Criterion> getAllCriterions() {
		init();
		return allCriterions;
	}

	public void setAllCriterions(List<Criterion> allCriterions) {
		this.allCriterions = allCriterions;
	}

	public Criterion findById(BigDecimal id) {
		Criterion c = null;
		try {
			ic = ContextHelper.getInitialContext();
			sysStringLocal = ContextHelper.injectSysStringLocal(ic);
			c = em.find(Criterion.class, id);
			if (c != null)
				c.setDescription(sysStringLocal.findByPk(new SysStringPk(c.getNameStrId(), LanguageSession.langId)).getValue());
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return c;
	}

	public Criterion getDefaultCriterion() {
		Criterion defaultCrit = null;
		try {
			defaultCrit = (Criterion) em.createQuery("FROM " + Criterion.class.getName() + " c WHERE c.isDefault>0").getSingleResult();
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return defaultCrit;
	}

}
